package com.itheima.sh.b_response_02;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/responseDemo03Servlet")
public class ResponseDemo03Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       /*
            需求：给浏览器响应字符数据。
            使用的方法： PrintWriter writer = resp.getWriter();
            1.如果服务器向浏览器响应中文会出现中文乱码问题，产生乱码的原因
                tomcat将响应的中文使用tomcat默认的编码表ISO-8859-1编码表进行编码，这个编码表不支持中文，对中文编码就会有问题
            2.解决问题：告知tomcat使用UTF-8编码，同时告知浏览器使用UTF-8
                方式一：
                    response.setHeader("content-type","text/plain;charset=utf-8");
                方式二：
                    response.setContentType("text/html;charset=utf-8");
            3.text/plain 表示普通文本类型，不解析html标签，如果想解析html标签：text/html,默认是text/html
        */
//        response.setHeader("content-type","text/html;charset=utf-8");
        response.setContentType("text/html;charset=utf-8");


        PrintWriter writer = response.getWriter();
        writer.write("itcast");
        writer.print("<br>");
        writer.write("<h1>HEIMA</h1>");

        writer.write("<h1>黑马</h1>");
    }
}
